﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.IndexMarkServiceReference;
using AdminModule.CommonClasses;

namespace AdminModule.Views
{
    public partial class SelectTermWindow : ChildWindow, IAddObjectSelector
    {
        private IndexMarkServiceClient client = new IndexMarkServiceClient();

        private int excludeIDTerm = 0;

        public SelectTermWindow()
        {
            InitializeComponent();
        }

        public SelectTermWindow(int excludeIDTerm)
        {
            InitializeComponent();
            this.excludeIDTerm = excludeIDTerm;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.termsComboBox.SelectedItem == null)
            {
                MessageBox.Show("Выберите период", MyAssemblyInfo.Title, MessageBoxButton.OK);
                return;
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.progressBar.IsVisibility = Visibility.Visible;
            client.GetAdmTermsCompleted += (s, ev) =>
                {
                    if(this.excludeIDTerm == 0)
                        termsComboBox.ItemsSource = ev.Result;
                    else
                        termsComboBox.ItemsSource = ev.Result.Where(w => w.IDTerm != this.excludeIDTerm);
                    this.progressBar.IsVisibility = Visibility.Collapsed;
                };
            client.GetAdmTermsAsync();
        }

        public DTOBase GetObject()
        {
            return this.termsComboBox.SelectedItem as DTOBase;
        }

        public IEnumerable<DTOBase> GetObjects()
        {
            throw new NotImplementedException();
        }
    }
}

